package com.gomtel.smartdevice.ui.activity.tools;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.moudle.db.entity.PulseConditionBean;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.ui.activity.MultiTypeActivity;
import com.gomtel.smartdevice.ui.adapter.AdapterStandardGeneral;
import com.gomtel.smartdevice.ui.view.AutoListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/19 0019.
 * 脉诊显示
 */
@SuppressWarnings("ALL")
public class PulseConditionLJFActvity extends WatchAppBaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    @BindView(R.id.img_btn_black)
    ImageButton img_btn_black;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.title_about_my)
    TextView title_about;
    @BindView(R.id.list)
    AutoListView listView;
    @BindView(R.id.drawerlayout)
    DrawerLayout drawerlayout;
    @BindView(R.id.right_list)
    ListView right_list;
    @BindView(R.id.new_time)
    TextView new_time;
    @BindView(R.id.ll_layout)
    LinearLayout ll_layout;
    @BindView(R.id.rl_new_time)
    RelativeLayout rl_new_time;

    private AdapterStandardGeneral adapterStandardGeneral;
    protected ArrayList<Object> list = new ArrayList<>();
    protected List<String> timeList = new ArrayList<>();
    private String newTime;

    @Override
    protected void registerEvent(CommonEvent commonevent) {

    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pulse_condition_ljf;
    }

    @Override
    protected void initClass() {

    }

    @Override
    protected void initData() {
        List<PulseConditionBean> pulseConditionBeenList = dataManager.queryPulseConditionList(UserContent.UserName);
        if (pulseConditionBeenList != null && pulseConditionBeenList.size() > 0) {
            list.add(pulseConditionBeenList.get(pulseConditionBeenList.size() - 3));
            list.add(pulseConditionBeenList.get(pulseConditionBeenList.size() - 2));
            list.add(pulseConditionBeenList.get(pulseConditionBeenList.size() - 1));
            newTime = pulseConditionBeenList.get(pulseConditionBeenList.size() - 1).getDay();
            refreshView();
        }
        if (pulseConditionBeenList != null) {
            for (int i = 0; i < pulseConditionBeenList.size(); i++) {
                timeList.add(pulseConditionBeenList.get(i).getDay());
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.simple_list_item, removeDuplicate(timeList));
            right_list.setAdapter(adapter);
        }
    }

    @Override
    protected void initView() {
        title_name.setText(getString(R.string.feel_the_pulse));
        title_about.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initListener() {
        title_about.setOnClickListener(this);
        img_btn_black.setOnClickListener(this);
        listView.setOnItemClickListener(this);
        right_list.setOnItemClickListener(rightOnItemClick);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_btn_black:
                finish();
                break;
            case R.id.title_about_my:
                drawerlayout.openDrawer(ll_layout);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PulseConditionBean pulseConditionBean = (PulseConditionBean) list.get(position);
        Intent intent = new Intent(this, MultiTypeActivity.class);
        intent.putExtra("ActionType", String.valueOf(EventCode.PULSE_CONDITION_CONTENT));
        Bundle bundle = new Bundle();
        bundle.putString("biaoxian", pulseConditionBean.getBiaoxian());
        bundle.putString("jianyi", pulseConditionBean.getJianyi());
        bundle.putString("maili", pulseConditionBean.getMaili());
        bundle.putString("pingji", pulseConditionBean.getPingji());
        bundle.putString("type", pulseConditionBean.getType());
        bundle.putString("zhengzhaung", pulseConditionBean.getZhengzhaung());
        bundle.putString("TitleAdvice", pulseConditionBean.getTitleAdvice());
        bundle.putString("Tupian", pulseConditionBean.getTupian());
        intent.putExtras(bundle);
        startActivity(intent);
    }

    private AdapterView.OnItemClickListener rightOnItemClick = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //toastUtil.showToast(timeList.get(position));
            list.clear();
            List<PulseConditionBean> pulseConditionBeen = dataManager.queryPulseConditionSelectTimeList(timeList.get(position));
            if (pulseConditionBeen != null) {
                for (PulseConditionBean pulseConditionBean : pulseConditionBeen) {
                    list.add(pulseConditionBean);
                    newTime = pulseConditionBean.getDay();
                }
            }
            refreshView();
            drawerlayout.closeDrawer(ll_layout);
        }
    };

    private void refreshView() {
        adapterStandardGeneral = new AdapterStandardGeneral(list, this);
        listView.setAdapter(adapterStandardGeneral);
        adapterStandardGeneral.notifyDataSetChanged();
        new_time.setText(new StringBuffer().append("最新时间:").append(newTime));
        rl_new_time.setVisibility(View.VISIBLE);
    }

    public List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }
}
